#              D I S T C L E A N . C M A K E . I N
# BRL-CAD
#
# Copyright (c) 2012-2025 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. The name of the author may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
###
# Clear benchmark files, if present
file(GLOB bench_files RELATIVE "@BRLCAD_BINARY_DIR@/bench" "@BRLCAD_BINARY_DIR@/bench/*")
if("${bench_files}" MATCHES "pix" OR "${bench_files}" MATCHES "log")
  execute_process(COMMAND "../@BIN_DIR@/benchmark" clobber WORKING_DIRECTORY "@BRLCAD_BINARY_DIR@/bench")
  execute_process(
    COMMAND ${CMAKE_COMMAND} -E remove -f "@BRLCAD_BINARY_DIR@/bench/summary"
  )
endif("${bench_files}" MATCHES "pix" OR "${bench_files}" MATCHES "log")

# Clear out the files the BRL-CAD build told us to clear
message("Removing CMake-generated files...")
set(clear_files "@CMAKE_DISTCLEAN_TARGET_LIST@")
file(REMOVE_RECURSE ${clear_files})
# Clean up after CPack
if(EXISTS "@BRLCAD_BINARY_DIR@/_CPack_Packages")
  file(REMOVE_RECURSE "@BRLCAD_BINARY_DIR@/_CPack_Packages")
endif(EXISTS "@BRLCAD_BINARY_DIR@/_CPack_Packages")
if(EXISTS "@BRLCAD_BINARY_DIR@/source_archive_contents")
  file(REMOVE_RECURSE "@BRLCAD_BINARY_DIR@/source_archive_contents")
endif(EXISTS "@BRLCAD_BINARY_DIR@/source_archive_contents")
message("Removing CMake-generated files... done.")

# Visual Studio solution file generated by CMake doesn't seem to produce
# a clean target msbuild is happy with.  (Maybe this issue?
# https://github.com/dotnet/msbuild/issues/3019) Until that can work,
# scrub manually.
if(EXISTS "@BRLCAD_BINARY_DIR@/BRLCAD.sln")
  file(GLOB_RECURSE VSFILES LIST_DIRECTORIES TRUE RELATIVE "@BRLCAD_BINARY_DIR@" "*")
  file(REMOVE_RECURSE ${VSFILES})
endif(EXISTS "@BRLCAD_BINARY_DIR@/BRLCAD.sln")

# Need to recursively go down the directories to get a full list.
# Easier to use a function, so maintain the directory list in
# a global property
function(FIND_ALL_DIRS seed_dir)
  file(GLOB dir_contents "${seed_dir}/*")
  foreach(content_file ${dir_contents})
    if(IS_DIRECTORY "${content_file}")
      get_property(ALL_DIRS_LIST GLOBAL PROPERTY ALL_DIRS_LIST)
      list(FIND ALL_DIRS_LIST "${content_file}" IN_LIST)
      if("${IN_LIST}" STREQUAL "-1")
        set_property(GLOBAL APPEND PROPERTY ALL_DIRS_LIST "${content_file}")
        find_all_dirs(${content_file})
      endif("${IN_LIST}" STREQUAL "-1")
    endif(IS_DIRECTORY "${content_file}")
  endforeach(content_file ${dir_contents})
endfunction(FIND_ALL_DIRS)

# Remove any empty directories - use CMake rather than
# find for portability - also, find was causing problems
# with filenames using spaces.
message("Removing empty directories in the build directory...")
file(GLOB remaining_files "@BRLCAD_BINARY_DIR@/*")
foreach(candidate ${remaining_files})
  if(IS_DIRECTORY "${candidate}")
    set_property(GLOBAL APPEND PROPERTY ALL_DIRS_LIST "${candidate}")
  endif(IS_DIRECTORY "${candidate}")
  get_property(ALL_DIRS_LIST_TOP GLOBAL PROPERTY ALL_DIRS_LIST)
  foreach(dir ${ALL_DIRS_LIST_TOP})
    find_all_dirs(${dir})
  endforeach(dir ${ALL_DIRS_LIST_TOP})
endforeach(candidate ${remaining_files})
set(HAVE_EMPTY_DIRS 1)
while(HAVE_EMPTY_DIRS)
  set(HAVE_EMPTY_DIRS 0)
  get_property(ALL_DIRS_LIST GLOBAL PROPERTY ALL_DIRS_LIST)
  foreach(candidate ${ALL_DIRS_LIST})
    if(NOT "${candidate}" MATCHES "\\.git")
      set(dir_contents)
      file(GLOB dir_contents "${candidate}/*")
      if(NOT dir_contents)
        list(REMOVE_ITEM ALL_DIRS_LIST "${candidate}")
        set_property(GLOBAL PROPERTY ALL_DIRS_LIST "${ALL_DIRS_LIST}")
        file(REMOVE_RECURSE "${candidate}")
        set(HAVE_EMPTY_DIRS 1)
      endif(NOT dir_contents)
    endif(NOT "${candidate}" MATCHES "\\.git")
  endforeach(candidate ${dir_list})
endwhile(HAVE_EMPTY_DIRS)
message("Removing empty directories in the build directory... done.")

# Done - now remove the output form of this script
file(REMOVE "@BRLCAD_BINARY_DIR@/distclean.cmake")

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
